package com.jianglin.state2.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jianglin.state2.core.ResponsePack;
import com.jianglin.state2.dao.entity.Employee;
import com.jianglin.state2.dao.entity.Project;

import com.jianglin.state2.model.core.PageParams;
import com.jianglin.state2.model.dto.ProjectCardDto;
import com.jianglin.state2.model.params.ProjectAddParams;
import com.jianglin.state2.model.params.ProjectUpdateParams;
import com.jianglin.state2.service.ProjectService;
import com.jianglin.state2.util.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@ResponsePack
@RequestMapping("/project")
@Api(tags = {"项目信息"})
public class ProjectController {


    private final ProjectService projectService;

    @Autowired
    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping("/all")
    @ApiOperation("获得所有项目信息")
    public List<Project> getAll(){
        return projectService.list();
    }

    @GetMapping("/count")
    @ApiOperation("获取项目数")
    public Integer getCount(){
        return projectService.count();
    }

    @GetMapping("/projectId/{projectId}")
    @ApiOperation("根据项目ID查找项目")
    public Project getProjectById(@ApiParam("项目ID") @PathVariable Integer projectId){
        return projectService.listByMap(SqlUtil.getColumnMap("projectId",projectId)).get(0);
    }

    @PostMapping("/add")
    @ApiOperation("添加新的项目信息")
    public boolean saveProject(@Validated @RequestBody ProjectAddParams projectAddParams) {
        Project project = projectAddParams.convertOut();
        return projectService.save(project);
    }

    @GetMapping("/clientId/{clientId}")
    @ApiOperation("根据客户ID查找项目")
    public Project getProjectByclientId(@ApiParam("客户ID") @PathVariable Integer clientId){
        return projectService.listByMap(SqlUtil.getColumnMap("clientId",clientId)).get(0);
    }

    @GetMapping("/name")
    @ApiOperation("获取所有卡片数据")
    public List<ProjectCardDto> getAllCardProject() {
        return projectService.list(new QueryWrapper<Project>()
                .select("projectId", "projectForm","projectName","beizu","projectComplete"))
                .stream()
                .map(project -> new ProjectCardDto().convertIn(project))
                .collect(Collectors.toList());
    }

    @GetMapping("/name/{clientId}")
    @ApiOperation("获取所有卡片数根据客户")
    public List<ProjectCardDto> getAllCardProjectByClientId(@ApiParam("客户ID") @PathVariable Integer clientId) {
        return projectService.selectByClient(clientId)
                .stream()
                .map(project -> new ProjectCardDto().convertIn(project))
                .collect(Collectors.toList());
    }

    @PutMapping("/{id}")
    @ApiOperation("更新项目信息")
    public boolean updateProject(@ApiParam("项目编号") @PathVariable Integer id,
                                 @Validated @RequestBody ProjectUpdateParams projectUpdateParams){
        Project project = projectUpdateParams.convertOut();
        project.setProjectId(id);
        return projectService.updateById(project);
    }

    @DeleteMapping({"/{id}"})
    @ApiOperation("删除项目")
    public boolean deleteProject(@ApiParam("项目编号") @PathVariable Integer id){
        return projectService.removeById(id);
    }
}